package database.alarm;

import java.util.Vector;

import object.alarm.Alarm;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class AlarmDelegate{
	private SQLiteDatabase database;
	private DBHelper dbHelper;
	private String[] allColumns = { DBHelper.KEY_ID, DBHelper.KEY_TIME, DBHelper.KEY_DAY, DBHelper.KEY_DATE, DBHelper.KEY_TYPE,
										DBHelper.KEY_STATUS, DBHelper.KEY_WORK, DBHelper.KEY_AUDIOALARM };

	public AlarmDelegate(Context context) {
		dbHelper = new DBHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}
	
	private Alarm cursorToAlarm(Cursor cursor){
		Alarm alarm = new Alarm();
		alarm.setId(cursor.getInt(0));
		alarm.setTime(cursor.getString(1));
		alarm.setDay(cursor.getString(2));
		alarm.setDate(cursor.getString(3));
		boolean type;
		if(cursor.getInt(4)==1) type = true;
		else type = false;
		alarm.setType(type);
		boolean stt;
		if(cursor.getInt(5)==1) stt = true;
		else stt = false;
		alarm.setStatus(stt);
		alarm.setWork(cursor.getString(6));
		alarm.setAudio(cursor.getString(7));
		return alarm;
	}
	
	public long addAlarm(Alarm alarm){
		ContentValues inititalValues = new ContentValues();
		inititalValues.put(DBHelper.KEY_TIME, alarm.getTime());
		int type;
		if(alarm.isType()){
			type=1;
			inititalValues.put(DBHelper.KEY_DAY, alarm.getDay());
		}else{
			type=0;
			inititalValues.put(DBHelper.KEY_DATE, alarm.getDate());
		}
		inititalValues.put(DBHelper.KEY_TYPE, type);
		inititalValues.put(DBHelper.KEY_STATUS, 1);
		inititalValues.put(DBHelper.KEY_WORK, alarm.getWork());
		inititalValues.put(DBHelper.KEY_AUDIOALARM, alarm.getAudio());
		return database.insert(DBHelper.DATABASE_TABLE, null, inititalValues);
	}
	
	public boolean editAlarm(Alarm alarm){
		ContentValues inititalValues = new ContentValues();
		inititalValues.put(DBHelper.KEY_TIME, alarm.getTime());
		inititalValues.put(DBHelper.KEY_DAY, alarm.getDay());
		inititalValues.put(DBHelper.KEY_DATE, alarm.getDate());
		inititalValues.put(DBHelper.KEY_WORK, alarm.getWork());
		inititalValues.put(DBHelper.KEY_AUDIOALARM, alarm.getAudio());
		int type;
		if(alarm.isType()) type=1;
		else type=0;
		inititalValues.put(DBHelper.KEY_TYPE, type);
		int status;
		if(alarm.isStatus()) status=1;
		else status=0;
		inititalValues.put(DBHelper.KEY_STATUS, status);
		return database.update(DBHelper.DATABASE_TABLE, inititalValues, DBHelper.KEY_ID +"="+alarm.getId(), null)>0;
	}
	
	public boolean editStatusAllAlarm(boolean status){
		ContentValues inititalValues = new ContentValues();
		int _status=1;
		if(status==false) _status=0;
		inititalValues.put(DBHelper.KEY_STATUS, _status);
		return database.update(DBHelper.DATABASE_TABLE, inititalValues, null, null)>0;
	}
	
	public boolean deleteAlarm(long id){
		return database.delete(DBHelper.DATABASE_TABLE, DBHelper.KEY_ID + "=" + id, null) >0;
	}
	
	public Vector<Alarm> getAllAlarm(){
		Vector<Alarm> listAlarms = new Vector<Alarm>();
		Cursor cursor = database.query(DBHelper.DATABASE_TABLE, allColumns,	null, null, null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Alarm alarm = cursorToAlarm(cursor);
			listAlarms.add(alarm);
			cursor.moveToNext();
		}
		cursor.close();
		return listAlarms;
	}
	
	public Alarm getAlarm(long id){
		Cursor cursor = database.query(DBHelper.DATABASE_TABLE,	allColumns, DBHelper.KEY_ID + " = " + id, null,	null, null, null);
		cursor.moveToFirst();
		Alarm alarm = cursorToAlarm(cursor);
		cursor.close();
		return alarm;
	}
	
}